//
//  MyScrollView.m
//  UI_Xman终极版
//
//  Created by BayMax on 15/5/2.
//  Copyright (c) 2015年 BayMax. All rights reserved.
//

#import "MyScrollView.h"
#import "TouchImage.h"
#import "RecFirModel.h"
#import <SDWebImage/UIImageView+WebCache.h>

@interface MyScrollView()<UIScrollViewDelegate>

@property(nonatomic, retain)UIScrollView *scrollView;
@property (nonatomic, retain) NSTimer *timer;
@property (nonatomic, assign) NSInteger page;
@property(nonatomic, retain) UIPageControl *pagePoint;

@end


@implementation MyScrollView

- (void)dealloc
{
    [_timer release];
    [_scrollView release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height - 25)];
        _scrollView.delegate = self;
        self.scrollView.pagingEnabled = YES;
        self.scrollView.showsHorizontalScrollIndicator = NO;
        [self addSubview:self.scrollView];
        [_scrollView release];
        
        self.pagePoint = [[UIPageControl alloc]initWithFrame:CGRectMake(self.scrollView.frame.size.width - 140, self.scrollView.frame.size.height + 5, 140, 20)];
//        [self.pagePoint setBackgroundColor:[UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:0.2]];
        self.pagePoint.pageIndicatorTintColor = [UIColor lightGrayColor];
        self.pagePoint.currentPageIndicatorTintColor = [UIColor blueColor];
        [self.pagePoint setNumberOfPages:5];
        [self addSubview:self.pagePoint];
        [_pagePoint release];

    }
    return self;
}

- (void) setScrollViewIamage:(NSArray *)array
{
    NSMutableArray *myArray = [NSMutableArray arrayWithArray:array];
    id first = [myArray firstObject];
    id last = [myArray lastObject];
    [myArray addObject:first];
    [myArray insertObject:last atIndex:0];
    
    for (int i = 0; i < [myArray count]; i++) {
        
        RecFirModel *model= [myArray objectAtIndex:i];
    
        TouchImage *imageView = [[TouchImage alloc]initWithFrame:CGRectMake(self.frame.size.width * i, 0, self.frame.size.width, self.frame.size.height - 25)];
        imageView.model = model;
        [imageView sd_setImageWithURL:[NSURL URLWithString:imageView.model.imgURL] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
        [self.scrollView addSubview:imageView];
        [imageView release];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
        [imageView addGestureRecognizer:tap];
        [tap release];
        
    }
    [self.scrollView setContentSize:CGSizeMake(myArray.count * self.scrollView.frame.size.width, 0)];
    
    [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width, 0)];
    
    if (self.timer) {
        [self.timer invalidate];
        self.timer = nil;
    }
    self.timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(timerAction:) userInfo:nil repeats:YES];
}

- (void)timerAction:(NSTimer *)timer
{
    self.page++;
    CGFloat offWidth = self.scrollView.frame.size.width * self.page;
    NSInteger num = self.scrollView.contentSize.width / self.scrollView.frame.size.width;
    
    [UIView animateWithDuration:0.5 animations:^{
        [self.scrollView setContentOffset:CGPointMake(offWidth, 0)];
    }];
    
    if (self.page == num - 1) {
        self.page = 1;
        [self.scrollView setContentOffset:CGPointMake(self.scrollView.frame.size.width, 0)];
    }
    
    
}


- (void)tapAction:(UITapGestureRecognizer *)tap
{
    TouchImage *view = (TouchImage *)tap.view;
    [self.delegate imageTouch:view];
    
    
}

//设置偏移量
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    CGPoint offset = scrollView.contentOffset;
    CGFloat width = scrollView.frame.size.width;
    CGSize contentSize = scrollView.contentSize;
    
    if (offset.x == contentSize.width - width) {
        [scrollView setContentOffset:CGPointMake(width, 0)];
    } else if (offset.x == 0){
        [scrollView setContentOffset:CGPointMake(contentSize.width - width * 2, 0)];
    }
    
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    NSInteger page = scrollView.contentOffset.x / scrollView.frame.size.width;
    self.pagePoint.currentPage = page - 1;
}


@end
